<?php

namespace app\common\logic;

use app\common\service\NodeService;
use think\Db;

class NodeLogic
{

    /**
     * @Notes:获取应用节点数据
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $app string
     * @return array
     */
    public static function getAppNodes($app)
    {
        $nodeService = new NodeService();
        $nodeService->setApp($app);
        $nodeService->setDir(app_path() . $app . DIRECTORY_SEPARATOR . 'controller');
        $nodeService->setNamespacePrefix("app\\{$app}\\controller");
        return $nodeService->getNodelist();
    }

    /**
     * @Notes:导入节点
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $apps array
     * @return array
     */
    public static function importNode($apps)
    {
        $newNodes = [];

        foreach ($apps as $app) {

            $nodeLists = NodeLogic::getAppNodes($app);

            if ($nodeLists) {

                $newNodes[$app] = [];

                foreach ($nodeLists as $k => $v) {

                    //检测节点是否存在
                    $node = Db::name("auth_rule")->where("name", $v["name"])->find();

                    if (!empty($node)) {
                        // array_push($newNodes[$app], $v['name'] . "--已导入");

                    } else {
                        if($v['parent']=="default")
                        {
                            $result = Db::name("auth_rule")->insert([
                                "title"     => $v['title'],
                                "name"      => $v["name"],
                                "status"    => $v['status'],
                                "type"      => $v['type'],
                                "parent_id" =>  0,
                                "condition" => $v["condition"],
                                "is_view"   => $v["is_view"],
                                "is_menu"   => $v["is_menu"],
                            ]);

                            if ($result) {

                                array_push($newNodes[$app], $v['name'] . "--导入成功");

                            } else {

                                array_push($newNodes[$app], $v['name'] . "--导入失败");

                            }
                        }else{
                            $sonNodes[] = $v;
                            //层级检测
                            $NodeParent = Db::name("auth_rule")->where("name", $v["parent"])->field("id")->find();
                            //新增节点

                            $result = Db::name("auth_rule")->insert([
                                "title"     => $v['title'],
                                "name"      => $v["name"],
                                "status"    => $v['status'],
                                "type"      => $v['type'],
                                "parent_id" => $NodeParent ? $NodeParent["id"] : 0,
                                "condition" => $v["condition"],
                                "is_view"   => $v["is_view"],
                                "is_menu"   => $v["is_menu"],
                            ]);

                            if ($result) {

                                array_push($newNodes[$app], $v['name'] . "--导入成功");

                            } else {

                                array_push($newNodes[$app], $v['name'] . "--导入失败");
                            }
                        }
                    }
                }
             /*   if(isset($sonNodes) && !empty($sonNodes)){

                    foreach ($sonNodes as $v)
                    {
                        //层级检测
                        $NodeParent = Db::name("auth_rule")->where("name", $v["parent"])->field("id")->find();
                        //新增节点

                        if( Db::name("auth_rule")->where("name", $v["name"])){}

                        $result = Db::name("auth_rule")->insert([
                            "title"     => $v['title'],
                            "name"      => $v["name"],
                            "status"    => $v['status'],
                            "type"      => $v['type'],
                            "parent_id" => $NodeParent ? $NodeParent["id"] : 0,
                            "condition" => $v["condition"],
                            "is_view"   => $v["is_view"],
                            "is_menu"   => $v["is_menu"],
                        ]);

                        if ($result) {

                            array_push($newNodes[$app], $v['name'] . "--导入成功");

                        } else {

                            array_push($newNodes[$app], $v['name'] . "--导入失败");

                        }
                    }
                }*/
            }
        }
        return $newNodes;
    }


    /**
     * @Notes:更新系统节点
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $apps array
     * @return array
     */
    public static function  updateNode($apps)
    {
        $newNodes = [];

        foreach ($apps as $app) {

            $nodeLists = NodeLogic::getAppNodes($app);

            if ($nodeLists) {

                $newNodes[$app] = [];

                foreach ($nodeLists as $k => $v) {

                    //检测节点
                    $node = Db::name("auth_rule")->where("name", $v["name"])->find();

                    if ($node) {

                        //节点层级变化
                        $NodeParent = Db::name("auth_rule")->where("name", $v["parent"])->find();

                        $result = Db::name("auth_rule")->where("id", $node['id'])->update([
                            "title"     => $v['title'],
                            "name"      => $v["name"],
                            "status"    => $v['status'],
                            "type"      => $v['type'],
                            "parent_id" => $NodeParent['id'] ? $NodeParent['id'] : 0,
                            "condition" => $v["condition"],
                            "is_view"   => $v["is_view"],
                            "is_menu"   => $v["is_menu"],
                            "auth"      => $v["auth"],
                        ]);

                        if ($result) {

                            array_push($newNodes[$app], $v['name'] . "--已更新");

                        } else {

                            array_push($newNodes[$app], $v['name'] . "--无需更新");

                        }
                    }else{

                        array_push($newNodes[$app], $v['name'] . "--需要导入");

                    }
                }
            }
        }
        return $newNodes;
    }


    /**
     * @Notes:处理节点,传入所需要处理的节点数据，返回处理过节点数据，status=1的为正常，0 的异常
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $data
     * @return mixed
     */
    public function processNode($data)
    {
        $nodeService = new NodeService();
        $apps = array_column($data, "name");
        foreach ($apps as $app) {
            $newApp = explode("/", $app);
            if ($newApp) {
                $appList[] = $newApp[0];
            }
        }
        if (isset($appList) && !empty($appList))
        {
            //去从
            $appList = array_unique($appList);
            if (isset($data) && !empty($data))
            {
                foreach ($appList as $app)
                {
                    if(is_dir(app_path() . $app))
                    {
                        $nodeService->setNamespacePrefix("app\\".$app."\\controller");
                        $nodeService->setNamespacePrefix("app\\".$app."\\controller");
                        $nodeService->setApp($app);
                        $nodeService->setDir(app_path() . $app . DIRECTORY_SEPARATOR . 'controller');
                        $nodeLists[$app] = array_column($nodeService->getNodelist(), "name",'name');
                    }
                }
                foreach ($data as $k => $node) {
                    $app = explode("/",$node['name']);
                   /* if (!in_array($node['name'], $nodeLists[$app])) {
                        $data[$k]['status'] = 0;
                    }*/
                    if (!isset($nodeLists[$app[0]][$node['name']])) {
                         $data[$k]['status'] = 0;
                    }
                }
            }
        }
        return $data;
    }


}